/*
 * mox_module.h
 *
 *  Created on: Jun 9, 2010
 *      Author: Jason Monk
 */

#ifndef MOX_MODULE_H_
#define MOX_MODULE_H_
#ifndef __XC__
#include <xccompat.h>
#endif

#include "mox.h"
#include "mox_slave.h"
#include "mox_storage.h"
#include "mox_threads.h"

/*
typedef enum {
	MOD_MSG=0x400,
	MOD_INVALID,
	MOD_LAUNCH,
	MOD_UPDATE,
	MOD_BUSY,
	MOD_READY,
	MOD_TYPE,
	MOD_STORAGE,
	MOD_GEN,
	MOD_ACK,
	MOD_NACK,
} mod_msg_t;
*/

struct mox_module {
	uchanend core_id;
	int mod_id;
};

struct mox_smod {
	char name[8];
#ifdef __XC__
	unsigned start_addr[NUM_CORES];
#else
	int *start_addr[NUM_CORES];
#endif
	int offset[NUM_CORES];
	int size;
	int stack[NUM_CORES];
	int const_addr[NUM_CORES];
	int data_addr[NUM_CORES];
};

typedef struct mox_rthd {
#ifdef __XC__
	unsigned thd_info;
#else
	struct mox_smod *thd_info;
#endif
	mox_msg_t type;
} mox_rthd;

struct module_at_start {
	int stack_size;
#ifdef __XC__
	unsigned prog_space;
	unsigned const_addr;
	unsigned data_addr;
#else
	int *prog_space;
	int *const_addr;
	int *data_addr;
#endif
};

#ifndef __XC__
int load_module(mox_master_state *state, const char module_name[]);
int init_module_start(mox_master_state *state, int core_num,
		struct module_at_start *mod, char *name);
struct mox_smod *check_module(struct mox_module *mox_mod,
		const char module_name[], int addr, int func_offset);
int get_mod_storage(struct mox_smod *mod, struct mox_module *mox_mod, void **p);

int start_module(struct mox_smod *mod, mox_sh id,
		mox_master_state *state, void *p);

void *m_malloc(chanend mox, int n);

void m_free(chanend mox, void *p);

#define THREAD_START_GET_CHAN(x) asm("stw r10,%0[0]"::"r"(&x))

int mod_thd_start_release(chanend mod, struct mox_slave_state *state);
#endif

#endif /* MOX_MODULE_H_ */
